#pragma once

#include "Ts.h"
#include "Mybs.h"

#ifndef LOG_TAG
#define  LOG_TAG   "TS_MUX"
#define  LOGI(...)  do { } while (0)
#endif

#define  MAX_VIDEO_TAG_BUF_SIZE   1024 * 1024
#define  VIDEO_TAG_HEADER_LENGTH  11


//H264一帧数据的结构体
typedef struct Tag_NALU_t
{
    unsigned char forbidden_bit;           //! Should always be FALSE
    unsigned char nal_reference_idc;       //! NALU_PRIORITY_xxxx
    unsigned char nal_unit_type;           //! NALU_TYPE_xxxx
    unsigned int  startcodeprefix_len;      //! 前缀字节数
    unsigned int  len;                     //! 包含nal 头的nal 长度，从第一个00000001到下一个000000001的长度
    unsigned int  max_size;                //! 做多一个nal 的长度
    unsigned char * buf;                   //! 包含nal 头的nal 数据
    unsigned char Frametype;               //! 帧类型
    unsigned int  lost_packets;            //! 预留
} NALU_t;

//nal类型
enum nalu_unit_type_e
{
    NALU_UNKNOWN = 0,
    NALU_SLICE = 1,
    NALU_SLICE_DPA = 2,
    NALU_SLICE_DPB = 3,
    NALU_SLICE_DPC = 4,
    NALU_SLICE_IDR = 5,      /* ref_idc != 0 */
    NALU_SEI = 6,            /* ref_idc == 0 */
    NALU_SPS = 7,
    NALU_PPS = 8
    /* ref_idc == 0 for 6,9,10,11,12 */
};

enum nal_unit_h265_type_e
{
	NALU_TRAIL_N = 0,
	NALU_TRAIL_R = 1,
	NALU_TSA_N = 2,
	NALU_TSA_R = 3,
	NALU_STSA_N = 4,
	NALU_STSA_R = 5,
	NALU_RADL_N = 6,
	NALU_RADL_R = 7,
	NALU_RASL_N = 8,
	NALU_RASL_R = 9,
	NALU_BLA_W_LP = 16,
	NALU_BLA_W_RADL = 17,
	NALU_BLA_N_LP = 18,
	NALU_IDR_W_RADL = 19,
	NALU_IDR_N_LP = 20,
	NALU_CRA_NUT = 21,
	NALU_VPS_ = 32,
	NALU_SPS_ = 33,
	NALU_PPS_ = 34,
	NALU_AUD = 35,
	NALU_EOS_NUT = 36,
	NALU_EOB_NUT = 37,
	NALU_FD_NUT = 38,
	NALU_SEI_PREFIX = 39,
	NALU_SEI_SUFFIX = 40
};

//帧类型
enum Frametype_e
{
    FRAME_I = 15,
    FRAME_P = 16,
    FRAME_B = 17
};


NALU_t *AllocNALU(int buffersize);										  //分配nal 资源
void FreeNALU(NALU_t * n);												  //释放nal 资源
int FindStartCode2 (unsigned char *Buf);								  //判断nal 前缀是否为3个字节
int FindStartCode3 (unsigned char *Buf);								  //判断nal 前缀是否为4个字节             
int GetAnnexbNALU(NALU_t *nalu, void *pData, int iDataSize, unsigned char uVideoStreamId);		//填写nal 数据和头
int GetFrameType(NALU_t * nal, unsigned char uVideoEncType);									//获取帧类型
int Read_One_H264_Frame(unsigned char * buf, unsigned int * videoframetype,void *pData,int iDataSize,unsigned char uVideoStreamId);
int VES2PES(TsPes * tsh264pes, unsigned long Videopts, unsigned int * videoframetype, unsigned char*pData, int iDataSize, unsigned char uVideoEncType);